package lanxing.future.lamda;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author <a href="mailto:lanxing@chances.com.cn">lanxing</a>
 * @version 2019年3月7日
 *
 */
public class Test {
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();
        Person person1 = new Person("a", new Date(1268318348000L), "上海");
        Person person2 = new Person("b", new Date(952785548000L), "北京");
        Person person3 = new Person("c", new Date(700324748000L), "上海");
        Person person4 = new Person("d", new Date(1268318348000L), "广州");

        persons.add(person1);
        persons.add(person2);
        persons.add(person3);
        persons.add(person4);

        // List遍历 （每个name后面都加一个@）
        persons.forEach(person -> {
            person.setName(person.getName() + "@");
        });
        persons.forEach(person -> {
            System.out.println(person.getName());
        });

        // 过滤（把上海的person打印出来）
        List<Person> personsFiltered = persons.stream().filter(person -> {
            return "上海".equals(person.getCity());
        }).collect(Collectors.toList());

        personsFiltered.forEach(person -> {
            System.out.println(person.getName() + ":" + person.getCity());
        });

        // 排序 （按生日，从小到大）
        List<Person> personsSorted = persons.stream().sorted((a, b) -> {
            return (int) (a.getBirthday().getTime() - b.getBirthday().getTime());
        }).collect(Collectors.toList());

        personsSorted.forEach(person -> {
            System.out.println(person.getName() + ":" + person.getBirthday());
        });

        // 按属性分组(按城市分)
        Map<String, List<Person>> cityGroup = persons.stream()
                .collect(Collectors.groupingBy(Person::getCity));
        cityGroup.forEach((key, value) -> {
            System.out.println("============分组：" + key);
            value.forEach(person -> {
                System.out.println(person.getName() + "-" + person.getCity());
            });
        });

        // 自定义分组(北京与广州作为一个北广分组)
        Map<Object, List<Person>> cityGroupSelf = persons.stream()
                .collect(Collectors.groupingBy(person -> {
                    if ("北京".equals(person.getCity()) || "广州".equals(person.getCity())) {
                        return "北广";
                    } else {
                        return person.getCity();
                    }
                }));
        cityGroupSelf.forEach((key, value) -> {
            System.out.println("============分组：" + key);
            value.forEach(person -> {
                System.out.println(person.getName() + "-" + person.getCity());
            });
        });
    }

    static class Person {
        private String name;
        private Date birthday;
        private String city;

        public Person() {

        }

        public Person(String name, Date birthday, String city) {
            this.birthday = birthday;
            this.name = name;
            this.city = city;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }

    }
}
